﻿#define _CRT_SECURE_NO_WARNINGS 1


#include <iostream>
using namespace std;

#include <vector>
#include <algorithm>

class Solution
{
public:
    vector<int> advantageCount(vector<int>& nums1, vector<int>& nums2)
    {
        int n = nums1.size();

        // 1. 排序
        sort(nums1.begin(), nums1.end());
        vector<int> index2(n);
        for (int i = 0; i < n; i++) index2[i] = i;
        sort(index2.begin(), index2.end(), [&](int i, int j)
            {
                return nums2[i] < nums2[j];
            });

        // 2. ⽥忌赛⻢
        vector<int> ret(n);
        int left = 0, right = n - 1;
        for (auto x : nums1)
        {
            if (x > nums2[index2[left]]) ret[index2[left++]] = x;
            else ret[index2[right--]] = x;
        }

        return ret;
    }
};


int main()
{
    vector<int> nums1 = { 2, 7, 11, 15 }, nums2 = { 1, 10, 4, 11 };

    auto v = Solution().advantageCount(nums1, nums2);

    for (auto e : v)
        cout << e << " ";

    cout << endl;

    return 0;
}